package com.spic.business.xfalarm.service.impl;

import com.spic.business.alarm.utils.DateTimeUtil;
import com.spic.business.xfalarm.domain.DeviceInfo;
import com.spic.business.xfalarm.mapper.DeviceInfoMapper;
import com.spic.business.xfalarm.mapper.OdsXfMapper;
import com.spic.business.xfalarm.service.IDeviceInfoService;
import com.spic.common.core.utils.StringUtils;
import lombok.SneakyThrows;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.SqlDateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 290512
 * @create 2022-03-02 15:26
 */
@Service
public class DeviceInfoServiceImpl implements IDeviceInfoService
{

    @Autowired
    private DeviceInfoMapper deviceInfoMapper;
    @Autowired
    private OdsXfMapper odsXfMapper;


    @SneakyThrows
    @Override
    public void queryDeviceInfoList()
    {
        //BeanUtils.populate 对象转换 时 日期处理
        ConvertUtils.register(new SqlDateConverter(null), Date.class);
        ConvertUtils.register(new Converter() {
            @Override
            public Object convert(Class type, Object value) {
                SimpleDateFormat simpleDateFormat2=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try{
                    if(value.toString().indexOf("-")!=-1){
                        return simpleDateFormat.parse(value.toString());
                    }else if(value.toString().indexOf("/")!=-1){
                        return simpleDateFormat2.parse(value.toString());
                    }else{
                        return null;
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }, Date.class);

        //消防1
        List<DeviceInfo> list = new ArrayList<>();

        List<Map<String, Object>> mapsList = deviceInfoMapper.getDeviceInfo();
        if(mapsList!=null && mapsList.size()>0){
            for(Map<String, Object> map : mapsList){
                DeviceInfo deviceInfo = new DeviceInfo();
                org.apache.commons.beanutils.BeanUtils.populate(deviceInfo,map);
                String addTIme = DateTimeUtil.dateToStr(deviceInfo.getCDI_AddTIme(), "yyyy-MM-dd HH:mm:ss");
                deviceInfo.setCDI_AddTIme(DateTimeUtil.strToDate(addTIme));
                list.add(deviceInfo);
                Map<String,Object> DeviceInfoXF = selectXFDatebyId(deviceInfo.getCDI_DeviceID());
                if(StringUtils.isNull(DeviceInfoXF)){
                    insertIntoXfData(deviceInfo);
                }
            }
        }
        //消防2
        List<DeviceInfo> list2 = new ArrayList<>();
        List<Map<String, Object>> mapsList2 = deviceInfoMapper.getDeviceInfo2();
        if(mapsList2!=null && mapsList2.size()>0){
            for(Map<String, Object> map : mapsList2){
                DeviceInfo deviceInfo = new DeviceInfo();
                BeanUtils.populate(deviceInfo,map);
                String addTIme = DateTimeUtil.dateToStr(deviceInfo.getCDI_AddTIme(), "yyyy-MM-dd HH:mm:ss");
                deviceInfo.setCDI_AddTIme(DateTimeUtil.strToDate(addTIme));
                list2.add(deviceInfo);
                Map<String,Object> DeviceInfoXF = selectXFDatebyId(deviceInfo.getCDI_DeviceID());
                if(StringUtils.isNull(DeviceInfoXF)){
                    insertIntoXfData(deviceInfo);
                }
            }
        }
    }

    public void insertIntoXfData(DeviceInfo deviceInfoXF)
    {
        int i = odsXfMapper.insertDeviceInfo(deviceInfoXF);
    }

    private Map<String,Object> selectXFDatebyId(String cdiDeviceID)
    {
        Map<String,Object> deviceInfo = odsXfMapper.selectDeviceInfoById(cdiDeviceID);
        return deviceInfo;

    }
}
